package b.gate.service

/**
  * Created by lenovo on 2017/1/1.
  */
import java.net.URLEncoder

import akka.actor.Props
import akka.event.slf4j.SLF4JLogging
import akka.http.scaladsl.marshalling.ToResponseMarshallable
import akka.http.scaladsl.model.{HttpEntity, _}
import akka.http.scaladsl.server.Route
import akka.stream.actor.ActorPublisher
import akka.stream.scaladsl.Source
import akka.util.ByteString
import b.gate.to.juhe.C天气服务方
import org.squbs.bottlemsgs._
import org.squbs.unicomplex.{RouteDefinition, WebContext}
import scala.concurrent.ExecutionContext.Implicits.global

class WeatherService extends RouteDefinition with WebContext with C天气服务方 {

  private[this] def echoEntity(v城市: String) = HttpEntity(ContentTypes.`text/html(UTF-8)`, s"""
            <html>
              <body>
                <h1>[$v城市]的天气怎么样？</h1>
              </body>
            </html>""")
  private[this] def juheEntity(v城市: String) = {
    val city = URLEncoder.encode(v城市, "UTF-8")
    val uri = s"/weather/index?cityname=$city&key=542bbe7c227fdd2ba8e812f99b4c239e"
    call(uri).map(HttpEntity(ContentTypes.`application/json`, _))
  }
  private[this] def juheEntityX(v城市: String) = {
    callx(v城市).map(HttpEntity(ContentTypes.`application/json`, _))
//      .map { f =>
//      println("f: " + f)
//      HttpEntity(ContentTypes.`application/json`, f.toString)
//    }
  }
  def route: Route =
    path("cities" / Segment ) { v城市: String =>
      get {
        complete(ToResponseMarshallable(juheEntityX(v城市)))
      }
    }
}
